package pri.damai.xiaowu.anydoor.business.login;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import lombok.Data;
import pri.damai.xiaowu.anydoor.business.BaseModel;
import pri.damai.xiaowu.anydoor.netty.ChannelStore;

/**
 *
 *
 * @description
 * @author YangXinFu
 * @date 9:55 2022/7/25
 **/
@Data
public class LoginReqModel implements BaseModel{
    private Integer userid;//用户ID
    private String username;//用户名称

    public Byte code() {
        return 1;//业务类型
    }

    @Override
    public void process(ChannelHandlerContext ctx) {

        Channel channel = ctx.channel();
        LoginResModel res = new LoginResModel();

        //从map里面根据用户ID获取连接通道
        Channel c = ChannelStore.getUserChannel(this.getUserid());

        if (c == null) {
            //通道为空，证明该用户没有在线

            //1.添加到map
            ChannelStore.setUserChannel(this.getUserid(), channel);

            //2.响应
            res.setStatus(0);
            res.setMsg("登录成功");
            res.setUserId(this.getUserid());
            res.writeAndFlush(ctx);
        } else {
            //通道不为空，证明该用户已经在线了
            res.setStatus(1);
            res.setMsg("该账户目前在线");
            res.writeAndFlush(ctx);
        }
    }
}
